﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;
using moose.extension;
using moose.irc;

namespace moose.irc.standard.actions
{
    /// <summary>
    /// Responds to Private Messages
    /// Well more specifically, it generates private message events, with the private message that came from the IRC Server.
    /// </summary>
    [CLSCompliant(true)]
    [Action(
        Name = "moose-std-rsp-privmsg",
        Description = "Responds to private messages",
        Author = "Mike MacKenzie-Jamer<email:mikejamer@gmail.com>",
        Version = "0.1",
        Type = ActionTypes.Response)]
    [Trigger(TriggerText = "PRIVMSG")]
    public sealed class PrivMsgResponse : IrcAction
    {
        public override IrcSessionStates valid_states()
        {
            return IrcSessionStates.Registered;
        }

        public override void action(IrcSession session, IrcMessage message)
        {
            //IF the source is null, then we assume the server is sending

            //TODO: generate entities here, so we can properly route this message...

            /**
             * Consider making an IrcServer type, diriving entity, and consider it the server..., for notices and the like...
             */

            IrcEntity source = session.get_entity(message.Source); 
            IrcEntity destination = session.get_entity(message.Arguments[0]);
            session.notify_event(createPrivateMessageEvent(source, destination, message.Arguments[1]));
            return;
        }

        /// <summary>
        /// Create a new Private Message Event
        /// </summary>
        /// <param name="source">Source of Message</param>
        /// <param name="destination">Destination of Message</param>
        /// <param name="message">Message itself</param>
        /// <returns>new proper event</returns>
        private IrcEvent createPrivateMessageEvent(IrcEntity source, IrcEntity destination, string message)
        {
            return new IrcEvent(IrcEventTypes.PrivateMessage,
                new IrcEventArgument(IrcEventArgumentType.SourceEntity, source),
                new IrcEventArgument(IrcEventArgumentType.DestinationEntity, destination),
                new IrcEventArgument(IrcEventArgumentType.Message, message));
        }

    }
}
